package dht.server;

import dht.IMetaServerRouteTable;
import dht.IRoutineHandler;
import dht.ITransactionHandler;
import dht.entity.Message;
import dht.entity.NodeInfo;
import dht.socket.SocketHandler;
import dht.table.MetaServerRouteTableImpl;
import dht.throwable.DhtException;

public class MetaServerImpl extends AbstractServer {

	protected IMetaServerRouteTable routeTable;

	public MetaServerImpl(NodeInfo localInfo) {
		super(localInfo);
		this.routeTable = new MetaServerRouteTableImpl(localInfo);
	}

	@Override
	public void handleInitiativeTransaction(NodeInfo nodeInfo, Message msg,
			boolean isSync) throws DhtException {
		ITransactionHandler transactionHandler = new MetaServerTransactionHandlerImpl(
				localInfo, nodeInfo, routeTable, tcpConnection);
		transactionHandler.handleInitiativeTransaction(msg, isSync);
	}

	@Override
	public void handlePassiveTransaction(SocketHandler socketHandler,
			boolean isSync) throws DhtException {
		ITransactionHandler transactionHandler = new MetaServerTransactionHandlerImpl(
				localInfo, socketHandler, routeTable, tcpConnection);
		transactionHandler.handlePassiveTransaction(isSync);
	}

	@Override
	public void startServer() {
		new Thread(this).start();
		IRoutineHandler routineHandler = new MetaServerRoutineHandlerImpl(this,
				routeTable);
		new Thread(routineHandler).start();
	}
}
